﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using DemoUserInfo.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using PagedList;

namespace DemoUserInfo.Controllers
{
    public class VideosController : Controller
    {
        public VideoDbContext db = new VideoDbContext();
        private VideoNums CountNums = new VideoNums();

        // GET: Videos
        public ActionResult Index(string tag, int? page)
        {
            var videos = from a in db.Videos select a;
            var tags = new List<string>();
            foreach (var temo in videos)
            {
                if (!tags.Contains(temo.Tag)) tags.Add(temo.Tag);
            }
            if (!String.IsNullOrEmpty(tag))
            {
                videos = videos.Where(a => a.Tag.Equals(tag));
            }
            ViewData["tags"] = tags;
            videos = videos.OrderByDescending(a => a.Date);
            int pageSize = 18;
            int pageNumber = (page ?? 1);
            return View(videos.ToPagedList(pageNumber, pageSize));
        }

        // GET: Videos/Details/3
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Video video = db.Videos.Find(id);
            if (video == null)
            {
                return HttpNotFound();
            }
            ViewBag.id = id;
            ViewBag.src = video.Source;
            ViewBag.titleImg = video.TitleImgSrc;
            return View(video);
        }
        // GET: Videos/Create

        // GET: Videos/Delete/3
        [Authorize]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Video video = db.Videos.Find(id);
            if (video == null)
            {
                return HttpNotFound();
            }
            return View(video);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult DeleteConfirmed(int id)
        {
            Video video = db.Videos.Find(id);
            if (video != null)
            {
                db.Videos.Remove(video);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}